package com.cloud.logger.zchain.zchainserver.service;

import com.cloud.logger.zchain.core.common.base.ResultMessage;
import com.cloud.logger.zchain.core.common.constant.ResultCodeConstant;
import com.cloud.logger.zchain.zchainserver.entity.ZchainOrgnation;
import com.cloud.logger.zchain.zchainserver.entity.ZchainUser;
import com.cloud.logger.zchain.zchainserver.entity.vo.ZchainOrgnationAndUserVo;
import com.cloud.logger.zchain.zchainserver.mongo.ZchianOrgnationRepository;
import com.cloud.logger.zchain.zchainserver.mongo.ZchianUserRepository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * User: wcy
 * Date: 2017/12/3
 * Time: 15:08
 */
@Service
public class ZchainOrgnationService {

    @Resource
    ZchianOrgnationRepository zchianOrgnationRepository;
    @Resource
    ZchianUserRepository userRepository;
    final static String USER ="user";
    final static String ORG ="org";

    public List<ZchainOrgnation> getZchainOrgnation(String parentId){
        List<ZchainOrgnation> orgnations = zchianOrgnationRepository.findByParentId(parentId);

        for(ZchainOrgnation orgnation : orgnations){
            orgnation.setChildren(getZchainOrgnation(orgnation.getId()));
        }
        return orgnations;
    }

    public ResultMessage findOrgAndUsers(){
        ResultMessage resultMessage = new ResultMessage();
        List<ZchainOrgnation> orgnations = zchianOrgnationRepository.findByParentId("-1");

        List<ZchainOrgnationAndUserVo> resultOrg = new ArrayList<>();
        for(ZchainOrgnation org : orgnations){
            ZchainOrgnationAndUserVo orgAndUser = new ZchainOrgnationAndUserVo();
            orgAndUser.setId(org.getId());
            orgAndUser.setType(ORG);
            orgAndUser.setName(org.getOrgName());
            List<ZchainOrgnationAndUserVo> list = getById(orgAndUser.getId(),new ArrayList<ZchainOrgnationAndUserVo>());
            orgAndUser.setChildren(list);
            resultOrg.add(orgAndUser);
        }
        resultMessage.setCode(ResultCodeConstant.SERVICE_SUCCESS_CODE);
        resultMessage.setContent(resultOrg);
        return resultMessage;
    }

    //递归，根据orgId获取组织用户树形结构
    List<ZchainOrgnationAndUserVo> getById(String orgId,List<ZchainOrgnationAndUserVo> list){
        List<ZchainOrgnation> orgnations = zchianOrgnationRepository.findByParentId(orgId);
        if(orgnations != null && orgnations.size() != 0){

            list = changeOrgnationToOrgAndUserVo(orgnations);
            for(ZchainOrgnationAndUserVo orgnation:list){
                List<ZchainUser> userList = userRepository.findByOrgId(orgnation.getId());
                List<ZchainOrgnationAndUserVo> userVoList = changeUserToOrgnationAndUserVo((ArrayList)userList);
                orgnation.setChildren(userVoList);
                getById(orgnation.getId(),list);
            }
        }
        return list;

    }

    List<ZchainOrgnationAndUserVo> changeUserToOrgnationAndUserVo(List<ZchainUser> userList){
        List<ZchainOrgnationAndUserVo> orgnationAndUserVoList = new ArrayList<ZchainOrgnationAndUserVo>();
        for(ZchainUser user:userList){
            ZchainOrgnationAndUserVo orgnationAndUserVo = new ZchainOrgnationAndUserVo();
            orgnationAndUserVo.setId(user.getId());
            orgnationAndUserVo.setType(USER);
            orgnationAndUserVo.setName(user.getEmployeeName());
            orgnationAndUserVoList.add(orgnationAndUserVo);
        }
        return orgnationAndUserVoList;
    }


    List<ZchainOrgnationAndUserVo> changeOrgnationToOrgAndUserVo(List<ZchainOrgnation> userList){
        List<ZchainOrgnationAndUserVo> orgnationAndUserVoList = new ArrayList<ZchainOrgnationAndUserVo>();
        for(ZchainOrgnation org:userList){
            ZchainOrgnationAndUserVo orgnationAndUserVo = new ZchainOrgnationAndUserVo();
            orgnationAndUserVo.setId(org.getId());
            orgnationAndUserVo.setType(ORG);
            orgnationAndUserVo.setName(org.getOrgName());
            orgnationAndUserVoList.add(orgnationAndUserVo);
        }
        return orgnationAndUserVoList;
    }

}
